package net.sudot.commons.utils;

import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeansException;
import org.springframework.beans.FatalBeanException;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;

import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.HashSet;
import java.util.List;

public class BeanUtils {

    /**
     * 添加Bean拷贝时过滤的属性
     *
     * @param $ignoreProperties 需要过滤的属性
     * @param ignoreProperties  添加新的需要过滤的属性
     * @return 返回需要过滤的默认的属性(注释头部包含的属性)和新添加的属性
     */
    public static HashSet<String> addIgnoreProperties(HashSet<String> $ignoreProperties, HashSet<String> ignoreProperties) {
        int $size = $ignoreProperties == null ? 0 : $ignoreProperties.size();
        int size = ignoreProperties == null ? 0 : ignoreProperties.size();
        HashSet<String> $ignore = new HashSet<String>($size + size, 1.0F);
        if ($size > 0) { $ignore.addAll($ignoreProperties); }
        if (size > 0) { $ignore.addAll(ignoreProperties); }
        return $ignore;
    }

    /**
     * 添加Bean拷贝时过滤的属性
     *
     * @param $ignoreProperties 需要过滤的属性
     * @param ignoreProperties  添加新的需要过滤的属性
     * @return 返回需要过滤的默认的属性(注释头部包含的属性)和新添加的属性
     */
    public static HashSet<String> addIgnoreProperties(HashSet<String> $ignoreProperties, List<String> ignoreProperties) {
        int $size = $ignoreProperties == null ? 0 : $ignoreProperties.size();
        int size = ignoreProperties == null ? 0 : ignoreProperties.size();
        HashSet<String> $ignore = new HashSet<String>($size + size, 1.0F);
        if ($size > 0) { $ignore.addAll($ignoreProperties); }
        if (size > 0) {
            for (String property : ignoreProperties) {
                $ignore.add(property);
            }
        }
        return $ignore;
    }

    /**
     * 添加Bean拷贝时过滤的属性
     *
     * @param $ignoreProperties 需要过滤的属性
     * @param ignoreProperties  添加新的需要过滤的属性
     * @return 返回需要过滤的默认的属性(注释头部包含的属性)和新添加的属性
     */
    public static HashSet<String> addIgnoreProperties(HashSet<String> $ignoreProperties, String... ignoreProperties) {
        int size = $ignoreProperties == null ? 0 : $ignoreProperties.size();
        int len = ignoreProperties == null ? 0 : ignoreProperties.length;
        HashSet<String> $ignore = new HashSet<String>(size + len, 1.0F);
        if (size > 0) { $ignore.addAll($ignoreProperties); }
        if (len > 0) {
            for (String property : ignoreProperties) {
                $ignore.add(property);
            }
        }
        return $ignore;
    }

    /**
     * Copy the property values of the given source bean into the given target bean,
     * ignoring the given "ignoreProperties".
     * <p>Note: The source and target classes do not have to match or even be derived
     * from each other, as long as the properties match. Any bean properties that the
     * source bean exposes but the target bean does not will silently be ignored.
     * <p>This is just a convenience method. For more complex transfer needs,
     * consider using a full BeanWrapper.
     *
     * @param source           the source bean
     * @param target           the target bean
     * @param ignoreProperties hashSet of property names to ignore
     * @throws BeansException if the copying failed
     * @see BeanWrapper
     */
    public static <T> T copyProperties(Object source, T target, HashSet<String> ignoreProperties) throws BeansException {

        Assert.notNull(source, "Source must not be null");
        Assert.notNull(target, "Target must not be null");

        Class<?> actualEditable = target.getClass();
        PropertyDescriptor[] targetPds = org.springframework.beans.BeanUtils.getPropertyDescriptors(actualEditable);

        for (PropertyDescriptor targetPd : targetPds) {
            Method writeMethod = targetPd.getWriteMethod();
            if (writeMethod != null && (ignoreProperties == null || (!ignoreProperties.contains(targetPd.getName())))) {
                PropertyDescriptor sourcePd = org.springframework.beans.BeanUtils.getPropertyDescriptor(source.getClass(), targetPd.getName());
                if (sourcePd != null) {
                    Method readMethod = sourcePd.getReadMethod();
                    if (readMethod != null &&
                            ClassUtils.isAssignable(writeMethod.getParameterTypes()[0], readMethod.getReturnType())) {
                        try {
                            if (!Modifier.isPublic(readMethod.getDeclaringClass().getModifiers())) {
                                readMethod.setAccessible(true);
                            }
                            Object value = readMethod.invoke(source);
                            if (!Modifier.isPublic(writeMethod.getDeclaringClass().getModifiers())) {
                                writeMethod.setAccessible(true);
                            }
                            writeMethod.invoke(target, value);
                        } catch (Throwable ex) {
                            throw new FatalBeanException(
                                    "Could not copy property '" + targetPd.getName() + "' from source to target", ex);
                        }
                    }
                }
            }
        }
        return target;
    }
}
